package com.hhkj.api.invoice.service.biz.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageInfo;
import com.hhkj.admin.invoice.common.cont.InvoiceConst;
import com.hhkj.admin.invoice.domain.Invoice;
import com.hhkj.admin.invoice.domain.InvoiceTitle;
import com.hhkj.admin.invoice.model.dopo.InvoicePo;
import com.hhkj.admin.invoice.service.base.InvoiceService;
import com.hhkj.admin.invoice.service.base.InvoiceTitleService;
import com.hhkj.admin.invoice.service.extend.InvoiceExtendService;
import com.hhkj.admin.order.domain.Order;
import com.hhkj.admin.order.service.base.OrderService;
import com.hhkj.api.invoice.model.req.ApiInvoicePagingReq;
import com.hhkj.api.invoice.model.req.ApiInvoiceReq;
import com.hhkj.api.invoice.model.res.ApiInvoiceDetailRes;
import com.hhkj.api.invoice.model.res.ApiInvoicePagingRes;
import com.hhkj.api.invoice.service.biz.ApiSaleInvoiceBizService;
import com.hhkj.api.sale.common.session.holder.ApiSaleSessionHolder;
import com.ruoyi.common.exception.GlobalException;
import com.ruoyi.common.utils.paging.PageInfoUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * ApiSaleInvoiceBizServiceImpl
 *
 * @author HHKJ-SERVER-ZJ
 * @since 2023/11/6 15:01
 */
@Service
public class ApiSaleInvoiceBizServiceImpl implements ApiSaleInvoiceBizService {

    @Autowired
    private InvoiceTitleService titleService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private InvoiceExtendService invoiceExtendService;
    @Autowired
    private InvoiceService invoiceService;

    @Override
    public ApiInvoiceDetailRes getById(Long id) {
        Long saleId = ApiSaleSessionHolder.getSaleId();
        InvoicePo invoicePo = invoiceExtendService.getPoByIdNotNull(id);
        if (!invoicePo.getSaleId().equals(saleId)) {
            throw new GlobalException("订单不存在，请刷新重试");
        }
        return BeanUtil.toBean(invoicePo, ApiInvoiceDetailRes.class);
    }

    @Override
    public PageInfo<ApiInvoicePagingRes> paging(ApiInvoicePagingReq req) {
        Long saleId = ApiSaleSessionHolder.getSaleId();
        req.setSaleId(saleId);
        List<InvoicePo> invoicePoList = invoiceService.listPoByDo(req);
        return PageInfoUtils.toPageInfo(invoicePoList, ApiInvoicePagingRes.class);
    }

    @Override
    public boolean save(ApiInvoiceReq req) {
        // 查看发票是否已经申请
        Invoice invoicePo
                = invoiceExtendService.getByOrderId(req.getOrderId());
        if (ObjectUtil.isNotNull(invoicePo)) {
            if (!InvoiceConst.Status.INVALID.getCode().equals(invoicePo.getStatusCode())) {
                throw new GlobalException("已申请或已开发票，请确认后重试");
            }
        }

        // 个人发票
        if (InvoiceConst.TitleTypeCode.PERSON.getCode().equals(req.getTitleTypeCode())) {
            if (StrUtil.isBlank(req.getTitle())) {
                throw new GlobalException("抬头不能为空");
            }
        }
        // 企业发票 要补充企业信息
        if (InvoiceConst.TitleTypeCode.ENTERPRISE.getCode().equals(req.getTitleTypeCode())) {
            if (ObjectUtil.isNull(req.getTitleId())) {
                throw new GlobalException("发票抬头不能为空");
            }
            InvoiceTitle titlePo = titleService.getById(req.getTitleId());
            if (ObjectUtil.isNull(titlePo)) {
                throw new GlobalException("发票抬头信息不存在，请重新确认");
            }
            req.setTitle(titlePo.getTitle());
            req.setTaxIdentificationNumber(titlePo.getTaxIdentificationNumber());
            req.setAddress(titlePo.getAddress());
            req.setMobile(titlePo.getMobile());
            req.setBankName(titlePo.getBankName());
            req.setBankAccount(titlePo.getBankAccount());
        }
        Long saleId = ApiSaleSessionHolder.getSaleId();
        // 获取订单信息
        Order orderPo = orderService.getById(req.getOrderId());
        if (ObjectUtil.isNull(orderPo) || !orderPo.getSaleId().equals(saleId)) {
            throw new GlobalException("订单不存在，请重新确认");
        }
        req.setTypeCode(InvoiceConst.TypeCode.ORDINARY_ELECTRONIC.getCode());
        req.setOrderNum(orderPo.getNum());
        req.setStatusCode(InvoiceConst.Status.NON.getCode());
        req.setSaleId(orderPo.getSaleId());
        req.setPrice(orderPo.getFinalPrice());
        // 保存
        return invoiceService.save(req);
    }
}
